/**
 * SubmitResp.java<br>
 *
 *
 * @version 下午10:12:44
 */
package com.iwechat.adapter.mina.entity;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * 下行消息响应对象
 *
 *
 */
public class SubmitResp extends PDUHeader {
    private static final int command = PDU.COMMAND_SUBMIT_RESP;
    public static final int  pkgLen  = PDU.HEAD_LEN + 4;
    /** 错误码 */
    private int              errorCode;

    /**
     * Constructors for send
     *
     *
     * @param submit
     * @param errorCode
     */
    public SubmitResp(PDUHeader submit, int errorCode) {
        this.sequence = submit.getSequence();
        this.timestamp = System.currentTimeMillis();
        this.errorCode = errorCode;
    }

    /**
     * Constructors for receive
     *
     *
     * @param header
     * @param errorCode
     */
    public SubmitResp(PDUHeader header, IoBuffer in) {
        this.sequence = header.getSequence();
        this.timestamp = header.getTimestamp();
        this.errorCode = in.getInt();
    }

    @Override
    public IoBuffer toIoBuffer(Charset charset) {
        IoBuffer buffer = IoBuffer.allocate(pkgLen);
        buffer.putInt(pkgLen);
        buffer.putInt(command);
        buffer.putInt(sequence);
        buffer.putLong(timestamp);
        buffer.putInt(errorCode);
        buffer.flip();
        return buffer;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    @Override
    public String toString() {
        return "SubmitResp [pkgLen=" + pkgLen + ", command=" + command + ", sequence=" + sequence + ", timestamp=" + timestamp + ", errorCode="
                + errorCode + "]";
    }
}
